<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use Common\Model\Cate;
use Common\Model\User;
use Common\Model\Goods;
use Common\Model\GoodsList;
use Common\Model\Order;
use Common\Model\OrderList;
use Common\Model\goodsAttr;


//测试控制器
class IndexController extends Controller{
	private $categoryModel;
	private $userModel;
	private $goodsModel;
	private $goodsListModel;
	private $orderModel;
	private $orderListModel;
	private $goodsAttrModel;
	
	
	
	//构造函数
	public function __init()
	{
		$this->categoryModel=new Cate;
		$this->userModel=new User;
		$this->goodsModel=new Goods;
		$this->goodsListModel=new GoodsList;
		$this->orderModel=new Order;
		$this->orderListModel=new OrderList;
		$this->goodsAttrModel=new GoodsAttr;

	}
	
    //动作
    public function index(){
    	// 一：热门搜索下面的数据
    	$searchData=$this->goodsModel->orderBy('click','desc')->limit(6)->get();
    	View::with('searchData',$searchData);
    	// p($searchData);
    	// 二：热卖的商品数据
    	$hotData=$this->goodsModel->orderBy('click','desc')->limit(4)->get();
    	
    	View::with('hotData',$hotData);


    	// 三：每层数据楼的数据
    	// 面包屑的数据，限制3条
    	$breadData=$this->categoryModel->where('pid=0')->limit(3)->get();
    	foreach ($breadData as $k => $v) {
    		$breadData[$k]['twomenu']=$this->categoryModel->where("pid={$v['cid']}")->limit(3)->get();
    	}
		// p($breadData);
    	View::with('breadData',$breadData);
    	// 面包屑下面的商品数据
    	foreach ($breadData as $k => $v) {
    		$breadGooodsData[$k]=$this->categoryModel->where("pid={$v['cid']}")->get();
    		foreach ($breadGooodsData[$k] as $kk => $vv) {
    			$temp=$this->goodsModel->where("shop_category_cid={$vv['cid']}")->get();
    			if($temp){
    				foreach ($temp as $kkk => $vvv) {
    					$showGoodsData[$k][]=$vvv;
    				}
    				
    			}
    		}
    	}	
    	View::with('showGoodsData',$showGoodsData);



    	//注意 ，在往首页调数据的时候，其实在控制器中我们已经要把想要循环的数据格式组合好了，就等前台去循环了，一且数据格式都尽量放到控制器中来完成
    	$cateData=$this->categoryModel->where('pid=0')->get();
		
		foreach($cateData as $k=>$v){
			$cateData[$k]['twomenu']=$this->categoryModel->where("pid={$v['cid']}")->get();
			$cateData[$k]['two_three']=$this->categoryModel->where("pid={$v['cid']}")->limit(3)->get();
		}
		
		
		
		foreach($cateData as $k=>$v){
			foreach($v['twomenu'] as $kk=>$vv) {
				$cateData[$k]['twomenu'][$kk]['threemenu']=$this->categoryModel->where("pid={$vv['cid']}")->get();
			}
		}	

		View::with('cateData',$cateData);

		// 看用户是否登录，如果登录的话，获得用户相应的信息
		if(isset($_SESSION['info'])){
			$userModel=new User;
			// p($_SESSION);
			$userData=$userModel->where("uid={$_SESSION['info']['uid']}")->find();
			View::with('userData',$userData);
		}

       View::make();






    }
}
